local assets = {
	Asset("ANIM", "anim/musha_bowm.zip"),
	Asset("ANIM", "anim/musha_bowm_swap.zip"),
	Asset("ANIM", "anim/musha_bowm_ready_swap.zip"),
	Asset("ANIM", "anim/musha_bowm_boost_swap.zip"),
	Asset("ANIM", "anim/musha_bowm_broken_swap.zip")
}

local function Updata(inst, display)
	if inst.components.musha_equipment:IsBroken() then
		inst.components.weapon:SetDamage(1)
		return
	end

	if not inst.is_bow then
		inst.components.weapon:SetDamage(28 + inst.components.musha_equipment.currentlevel * 2)
		if display then
			inst.components.talker:Say(
				string.format(
					STRINGS.MUSHA_WEAPON_SWORD_BOW .. " (LV%d)\n" .. STRINGS.MUSHA_WEAPON_DAMAGE .. " (%d)",
					inst.components.musha_equipment.currentlevel,
					28 + inst.components.musha_equipment.currentlevel * 2
				)
			)
		end
	else
		inst.components.weapon:SetDamage(35 + inst.components.musha_equipment.currentlevel)
		if display then
			inst.components.talker:Say(
				string.format(
					STRINGS.MUSHA_WEAPON_SWORD_BOW .. " (LV%d)\n" .. STRINGS.MUSHA_WEAPON_DAMAGE .. " (%d)",
					inst.components.musha_equipment.currentlevel,
					35 + inst.components.musha_equipment.currentlevel
				)
			)
		end
	end
end

local function fire_arrow(inst, attacker, target)
	if
		inst.is_bow and
			attacker.components.inventory:FindItem(
				function(item)
					return item.prefab == "arrow"
				end
			)
	 then
		attacker.components.inventory:ConsumeByName("arrow", 1)
	end

	if math.random() > inst.components.musha_equipment.currentlevel * 0.012 + 0.5 then
		target.components.health:SetInvincible(true)
		if math.random() < 0.5 then
			attacker.components.talker:Say("Missed ..")
		else
			attacker.components.talker:Say("Hey! let me introduce my edge boy..")
		end
		target:DoTaskInTime(
			0.3,
			function()
				target.components.health:SetInvincible(false)
			end
		)
	end

	if not attacker:HasTag("framea") and not attacker:HasTag("lightningblue1") and not attacker:HasTag("lightningblue2") then
		if
			math.random() < 0.7 and target.components.health and not target.components.health:IsDead() and target:HasTag("bird")
		 then
			target:DoTaskInTime(
				0.3,
				function()
					if not target.components.health:IsDead() then
						target.sg:GoToState("flyaway")
					end
				end
			)
		end

		if target:HasTag("rabbit") and target.components.health and not target.components.health:IsDead() then
			target:DoTaskInTime(
				0.3,
				function()
					target:PushEvent("gohome")
				end
			)
		end
	end
end

local function Energy_Switch(inst)
	local owner = inst.components.inventoryitem.owner
	if not inst.is_bow then
		if inst.components.musha_equipment:IsBroken() then
			return
		end

		inst.components.talker:Say("-" .. STRINGS.MUSHA_WEAPON_SWORD_BOW .. "\n" .. STRINGS.MUSHA_WEAPON_SWORD_POWER_OFF)
		if inst.components.weapon then
			inst.components.weapon:SetRange(11, 13)
			inst.components.weapon:SetProjectile("musha_arrowm_flying")
			inst.components.weapon:SetOnProjectileLaunch(fire_arrow)
			Updata(inst)
		end

		owner.AnimState:OverrideSymbol("swap_object", "musha_bowm_boost_swap", "bowm")

		local change_fx = SpawnPrefab("small_puff")
		local follower = change_fx.entity:AddFollower()
		follower:FollowSymbol(owner.GUID, "swap_object", 1, -100, 1)
	else
		inst.components.weapon:SetRange()
		inst.components.talker:Say("-" .. STRINGS.MUSHA_WEAPON_SWORD_BOW .. "\n" .. STRINGS.MUSHA_WEAPON_SWORD_POWER_OFF)
		owner.AnimState:OverrideSymbol("swap_object", "musha_bowm_swap", "bowm")
	end
	inst.is_bow = not inst.is_bow
	inst.SoundEmitter:PlaySound("dontstarve/common/fireAddFuel")

	inst.components.useableitem:StopUsingItem()
end

local function onattack_Flame(inst, attacker, target)
	if inst.is_bow then
		--check arrow
		local arrow =
			attacker.components.inventory:FindItem(
			function(item)
				return item.prefab == "musha_arrowm"
			end
		)

		if not arrow then
			Energy_Switch(inst)
			inst.components.talker:Say(STRINGS.MUSHA_WEAPON_NO_ARROWS)
			inst.SoundEmitter:PlaySound("dontstarve/common/fireOut")
			return
		end

		attacker.components.inventory:ConsumeByName("musha_arrowm", 1)
		inst.components.musha_equipment:DoDelta(-30)
		local fx = SpawnPrefab("firesplash_fx")
		fx.Transform:SetScale(0.4, 0.4, 0.4)
		fx.Transform:SetPosition(target:GetPosition():Get())
		if
			math.random() <
				((inst.components.musha_equipment.currentlevel > 20 and 0.6) or
					(inst.components.musha_equipment.currentlevel > 10 and 0.45) or
					0.35) and target.components.burnable
		 then
			target.components.burnable:Ignite()
			target.components.health:DoDelta(-20)
		end
		attacker.components.sanity:DoDelta(2)
	end
	if math.random() < 0.1 then
		inst.components.musha_equipment:Gain_Exp(1)
	end
end

local function OnEquip(inst, owner)
	--Updata(inst)
	owner.AnimState:OverrideSymbol("swap_object", "musha_bowm_swap", "bowm")

	owner.AnimState:Show("ARM_carry")
	owner.AnimState:Hide("ARM_normal")
end

local function OnUnequip(inst, owner)
	if inst.EnergyOn then
		Energy_Switch(inst)
	end

	owner.AnimState:Hide("ARM_carry")
	owner.AnimState:Show("ARM_normal")
end

local function fn()
	local inst = CreateEntity()

	inst.entity:AddTransform()
	inst.entity:AddAnimState()
	inst.entity:AddSoundEmitter()
	inst.entity:AddNetwork()

	inst.entity:AddLight()
	inst.Light:SetRadius(.4)
	inst.Light:SetFalloff(.5)
	inst.Light:SetIntensity(.5)
	inst.Light:SetColour(80 / 255, 120 / 255, 250 / 255)
	inst.Light:Enable(true)

	inst.AnimState:SetBank("bowm")
	inst.AnimState:SetBuild("musha_bowm")
	inst.AnimState:PlayAnimation("idle")
	inst.AnimState:SetMultColour(1, 1, 1, 0.7)

	inst:AddTag("sharp")
	inst:AddTag("aquatic")
	inst:AddTag("musha_items")

	inst:AddComponent("talker")
	inst.components.talker.fontsize = 30
	inst.components.talker.font = TALKINGFONT
	inst.components.talker.colour = Vector3(0.9, 1, 0.75, 1)
	inst.components.talker.offset = Vector3(0, 150, 0)
	inst.components.talker.symbol = "swap_object"

	MakeInventoryPhysics(inst)
	MakeInventoryFloatable(inst, nil, nil)

	inst.entity:SetPristine()

	if not TheWorld.ismastersim then
		return inst
	end

	inst:AddComponent("useableitem")
	inst.components.useableitem:SetOnUseFn(Energy_Switch)

	inst:AddComponent("equippable")
	inst.components.equippable:SetOnEquip(OnEquip)
	inst.components.equippable:SetOnUnequip(OnUnequip)

	inst:AddComponent("musha_equipment")
	inst.components.musha_equipment:SetUpgradable(false)
	inst.components.musha_equipment:InitMushaEquip("weapon", 750, 0)
	inst.components.musha_equipment:SetOnUpdataFn(Updata)
	inst.components.musha_equipment:SetOnBrokenFn(
		function()
			if inst.EnergyOn then
				Energy_Switch(inst)
				inst.EnergyOn = true
			end
			local owner = inst.components.inventoryitem.owner
			SpawnPrefab("splash").Transform:SetPosition(inst:GetPosition():Get())
			owner.components.talker:Say(STRINGS.MUSHA_WEAPON_BROKEN_TALK)
			inst.components.talker:Say("-" .. STRINGS.MUSHA_WEAPON .. "\n" .. STRINGS.MUSHA_ITEM_DUR .. " (0)")
			owner.AnimState:OverrideSymbol("swap_object", "musha_bowm_broken_swap", "bowm")
			Updata(inst)
		end
	)

	inst.components.inventoryitem.atlasname = "images/inventoryimages/musha_bowm.xml"

	inst.BuffAttack = onattack_Flame

	inst.is_bow = false
	inst.type = "fire"

	--[[ inst.Rebind = function(inst)
		if inst.EnergyOn then
			inst.EnergyOn = false
			Energy_Switch(inst)
		end
	end ]]
	return inst
end

return Prefab("musha_bowm", fn, assets)
